package com.xxkj.drugtraceability.ui.drugstorage

import android.content.Intent
import android.view.LayoutInflater
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.gson.Gson
import com.xxhis.refactor.eventbus.MessageCode
import com.xxkj.drugtraceability.bean.EventMessage
import com.xxkj.drugtraceability.R
import com.xxkj.drugtraceability.base.BaseVMActivity
import com.xxkj.drugtraceability.bean.BarcodeMessage
import com.xxkj.drugtraceability.databinding.ActivityDrugstorageBinding
import com.xxkj.drugtraceability.databinding.ActivityPrescriptiondrugcollectionBinding
import com.xxkj.drugtraceability.databinding.LayoutDrupTopViewBinding
import com.xxkj.drugtraceability.dialog.BlueDialog
import com.xxkj.drugtraceability.lifecycle.KtxManager
import com.xxkj.drugtraceability.scan.BarcodeType
import com.xxkj.drugtraceability.ui.drugscan.DrugScanActivity
import com.xxkj.drugtraceability.utils.toast
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import org.koin.androidx.viewmodel.ext.android.viewModel

/**
 * author: zhoujingjin
 * created on: 2024/9/10 14:43
 * description:药品入库采集
 */
class DrugStorageActivity:BaseVMActivity() {
    private val binding by binding<ActivityDrugstorageBinding>(R.layout.activity_drugstorage)
    private val viewModel by viewModel<DrugStorageViewModel>()
    private lateinit var adapter: DrugStorageAdapter
    override fun initView() {
        binding.apply {
            toolBar.title = ""
            setSupportActionBar(toolBar)
            toolBar.setNavigationOnClickListener {
                onBackPressed()
            }
            recyclerView.layoutManager = LinearLayoutManager(this@DrugStorageActivity)
            adapter = DrugStorageAdapter{
                //点击去到扫追溯码页面
                startActivityForResult(Intent(this@DrugStorageActivity,DrugScanActivity::class.java).putExtra("data",
                    it),2222)
            }
            recyclerView.adapter = adapter
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode==2222&&resultCode==3333){
            viewModel.queryDrugInStorageInfo(viewModel.barcode)
        }
    }
    override fun initData() {
        viewModel.dataList.postValue(mutableListOf())
    }

    override fun startObserve() {
        viewModel.dataList.observe(this, Observer {
            adapter.removeAllHeaderView()

            if (it?.isNotEmpty()==true){
                val view =   LayoutInflater.from(this).inflate(R.layout.layout_drup_top_view,null)
                val bindTop = DataBindingUtil.bind<LayoutDrupTopViewBinding>(view)
                bindTop?.apply {
                    tvNum.text = "入库单号：${it[0].inStorageNo}"
                    tvDate.text = "入库时间：${it[0].invChgTime}"
                }
                adapter.addHeaderView(view)
            }else{
                adapter.setEmptyView(R.layout.layout_nodata)
            }
            adapter.setList(it)
        })
        viewModel.uiState.observe(this, Observer {
            if (it.isLoading){
                showMyDialog(resources.getString(R.string.getting_data))
            }else{
                dismissMyDialog()
            }
            if (!it.isError.isNullOrEmpty()){
                BlueDialog(this,type = 1,content = it.isError!!){}
            }
        })
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onGetMessage(message: EventMessage?) {
        message?.apply {
            //扫码拿到barcode了，去请求接口
            when (message.messageCode) {
                MessageCode.SCAN_BARCODE -> {
                    if (KtxManager.isTopActivity(this@DrugStorageActivity)){
                        val barcodeMessage = this.any as BarcodeMessage
                            viewModel.queryDrugInStorageInfo(barcodeMessage.barcode)
                    }
                }
            }
        }
    }
    override fun onStart() {
        super.onStart()
        if (!EventBus.getDefault().isRegistered(this))
            EventBus.getDefault().register(this)
    }

    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }
}